#include "speech_server/workingthread.hpp"

WorkingThread::WorkingThread(QString worker_name,ComminucationHandler *communication,QObject *parent) :
    QThread(parent),comminucation_handler(communication),workerName(worker_name),mutex(new QMutex),
    result_recieved(new QWaitCondition())
{
    connect(comminucation_handler,SIGNAL(MessageRecieved(QString,QString)),this,SLOT(ResultRecieved(QString,QString)),Qt::DirectConnection);
}

QString WorkingThread::commitCommand(QString target,QString command)
{
    mutex->lock();
    {
        qDebug("fsdfds");
        comminucation_handler->sendCommand(target,command);
        result_recieved->wait(mutex);
    }
    mutex->unlock();
    return RecievedData;
}

void WorkingThread::ResultRecieved(QString target,QString message)
{
    if(workerName==target)
    {
        RecievedData=message;
        qDebug()<<":)"<<message;
        Q_EMIT Log("["+workerName+"] :"+ XmlProccessor::extractLogMessage(RecievedData));
        result_recieved->wakeAll();
    }
}

QString WorkingThread::getWorkerName() const
{
    return workerName;
}
